//
//  SHPBottomToolbar.swift
//  weibo
//
//  Created by songhuipan on 16/4/21.
//  Copyright © 2016年 songhuipan. All rights reserved.
//

import UIKit

class SHPBottomToolbar: UIView {
    var retweetButton:UIButton?
    var commentButton:UIButton?
    var unlikeButton:UIButton?

    
    var status :SHPStatus? {
        didSet{
            let retweetTitle = getButtonTitle((status?.reposts_count)!, title: "转发")
            let commentTitle = getButtonTitle((status?.comments_count)!, title: "评论")
            let attitudesTitle = getButtonTitle((status?.attitudes_count)!, title: "赞")
            retweetButton?.setTitle(retweetTitle, forState: UIControlState.Normal)
            commentButton?.setTitle(commentTitle, forState: UIControlState.Normal)
            unlikeButton?.setTitle(attitudesTitle, forState: UIControlState.Normal)
            
        }
        
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
extension SHPBottomToolbar {
    func setupUI(){
        backgroundColor = getColorWithRGB(237, green: 237, blue: 237)
        retweetButton = addButton("转发", image: UIImage(named: "timeline_icon_retweet")!)
        commentButton = addButton("评论", image: UIImage(named: "timeline_icon_comment")!)
        unlikeButton = addButton("赞", image: UIImage(named: "timeline_icon_unlike")!)
        retweetButton!.snp_makeConstraints { (make) -> Void in
            make.leading.top.bottom.equalTo(self)
            make.width.equalTo(commentButton!)
        }
        commentButton!.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(retweetButton!.snp_trailing)
            make.top.bottom.equalTo(self)
            make.width.equalTo(unlikeButton!)
        }
        unlikeButton!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(commentButton!.snp_trailing)
        }
        
        let lineImageViewOne = addlineImageView()
        let lineImageViewTwo = addlineImageView()
        lineImageViewOne.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(retweetButton!.snp_trailing)
            make.centerY.equalTo(retweetButton!)
        }
        lineImageViewTwo.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(unlikeButton!.snp_leading)
            make.centerY.equalTo(commentButton!)
        }
    }
    // MARK:设置转发评论赞按钮的标题方法
    func getButtonTitle(count:Int ,title:String)->String {
        if count <= 0 {
            return title
        }else if count > 0 && count < 10000 {
            return "\(count)"
        }else {
            // 以1000为界限，低于1000不统计
            let num = CGFloat(count/1000)/10
            var titleString = "\(num)万"
            if titleString.containsString(".0"){
                titleString = titleString.stringByReplacingOccurrencesOfString(".0万", withString: "万")
            }
            return titleString
        }
        
    }
    // MARK:创建按钮，并添加
    private func addButton (title:String,image:UIImage)->UIButton {
        let button = UIButton(type: .Custom)
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        button.setImage(image, forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        addSubview(button)
        return button
    }
    // MARK:创建按钮间的竖线
    private func addlineImageView ()->UIImageView {
        let lineImageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        lineImageView.highlightedImage = UIImage(named: "timeline_card_bottom_line_highlighted")
        addSubview(lineImageView)
        return lineImageView
    }
}












